<?
/**
* Subclassing ManyToMany to add self referential many to many
*/
class ManyToMany_selfref extends ManyToMany
{
	var $RelatedPage = "";
	
	// expects: $Caption, $LocalField, $ForeignTable, $ForeignKeyField, $ForeignDataField, $IntermediateTable, $IntermediateLocalKey, $IntermediateForeignKey
	function ManyToMany_selfref($Properties)
	{
		parent::ManyToMany($Properties);
	}
	
	/**
	* Renders a self-referential ManyToMany control.
	*
	* - QD_MODE_VIEW <br>
	*   This control will render a list of ForeignDataField seperated by $this->Delimeter.  
	* - QD_MODE_EDIT<br>
	*   Displays a multi-select list box, with the items from the join table selected.
	* - QD_MODE_ADD<br>
	*   Displays a multi-select list box, with no items selected.
	*
	* @todo add option to list columns of text boxes
	* @param array $DataRow
	* @param integer $DisplayMode QD_MODE_VIEW, QD_MODE_EDIT, QD_MODE_ADD
	*/
	function Render($DataRow, $DisplayMode = QD_MODE_VIEW)
	{
		//Render the caption
		$out .= "\t<tr>\n\t\t<td class=\"" . $this->Parent->ViewCaptionCssClassName . "\">";
		$out .= $this->Caption;
		if ($this->HelpText != '' && $DisplayMode != QD_MODE_VIEW)
		{
			$out .= "<br />$this->HelpText";
		}
		$out .= "</td>\n";

		// Render the HTML
		$out .= "\t\t<td class=\"";
		$out .= $this->Parent->ViewCssClassName . "\">";
		$DB = $this->Parent->Parent->DB;

		switch($DisplayMode)
		{
			case QD_MODE_VIEW:
				// Build the view query.
				$LocalKeyValue =  $this->getLocalKeyValue();
				$SQL = "SELECT {$this->ForeignDataField}, {$this->ForeignKeyField}";
				$SQL .= " FROM {$this->Parent->Parent->DBTableName}, ";
				$SQL .= "( SELECT {$this->IntermediateLocalKey} AS first, {$this->IntermediateForeignKey} AS second FROM {$this->IntermediateTable} WHERE {$this->IntermediateLocalKey} = {$LocalKeyValue}";
				$SQL .= " UNION ";
				$SQL .= " SELECT {$this->IntermediateForeignKey} AS first, {$this->IntermediateLocalKey} AS second FROM {$this->IntermediateTable} WHERE {$this->IntermediateForeignKey} = {$LocalKeyValue}";
				$SQL .= " ) AS self";
				$SQL .= " WHERE {$this->Parent->Parent->DBTableName}.{$this->LocalField} = self.second";
//				$out .= "$SQL<br>";
				// Execute the query
				$Query = $DB->CreateQuery($SQL);
				$out .= "<ul>";
				while($row = $Query->FetchRow())
				{
					$out .= "\t<li>\n";
					if ($this->RelatedPage != "")
					{
						$out .= "\t\t<a href=\"{$this->RelatedPage}?View=View&PrimaryKeyValue={$row[$this->ForeignKeyField]}\">{$row[$this->ForeignDataField]}</a>\n";
					}
					else
					{
						$out .= $row[$this->ForeignDataField];
					}
					$out .= "\t</li>\n";
				}
				$out .= "</ul>\n";
				break;
			case QD_MODE_EDIT: // first select all records from the join table; these will be our be our selections
				$LocalKeyValue =  $this->getLocalKeyValue();
				$SQL .= "SELECT second ";
				$SQL .= " FROM {$this->Parent->Parent->DBTableName}, ";
				$SQL .= " ( SELECT {$this->IntermediateLocalKey} AS first, {$this->IntermediateForeignKey} AS second FROM {$this->IntermediateTable} WHERE {$this->IntermediateLocalKey} = {$LocalKeyValue}";
				$SQL .= " UNION";
				$SQL .= " SELECT {$this->IntermediateForeignKey} AS first, {$this->IntermediateLocalKey} AS second FROM {$this->IntermediateTable} WHERE {$this->IntermediateForeignKey} = {$LocalKeyValue}";
				$SQL .= " ) AS self";
				$SQL .= " WHERE {$this->Parent->Parent->DBTableName}.{$this->LocalField} = self.first ";

//				$out .= "$SQL<br>";
				// Execute the query
				$Query = $DB->CreateQuery($SQL);
				while($row = $Query->FetchRow())
				{
					$selections[] = $row['second'];
				}
				//no break, we want to execute the next branch if in edit mode
			case QD_MODE_ADD:
				// then select all records from the foreign table, excluding ourself; this will be our list box
				$SQL = "SELECT {$this->ForeignKeyField}, {$this->ForeignDataField} ";
				$SQL .= " FROM {$this->ForeignTable} ";
				if (!is_null($this->ParentKeyName) && !is_null($this->ParentKeyValue))
				{
					$SQL .= " WHERE {$this->ParentKeyName} = {$this->ParentKeyValue} ";
				}
				$SQL .= " ORDER BY {$this->ForeignDataField} ";
//				$out .= "$SQL<br>";
				// Execute the query
				$Query = $DB->CreateQuery($SQL);
				$count = $Query->NumRows();
				$boxsize =  ($count > 10) ? 10 : $count;
				if ($count > 0 )
				{
					$out .= "\t\t<select name=\"{$this->Key}[]\" class=\"{$this->Parent->EditCssClassName}\" size=\"$boxsize\" multiple\">\n";
					while ($resultsRow = $Query->FetchRow())
					{
						$out .= "\t\t\t<option ";
						if (is_array($selections))
						{
							if (in_array($resultsRow[$this->ForeignKeyField], $selections))
							{
								$out .= "selected ";
							}
						}
						$out .= " value=\"" . $resultsRow[$this->ForeignKeyField] . "\">" . substr($resultsRow[$this->ForeignDataField],0,80) . "</option>\n";
					}
					$out .= "\t\t</select>";
				}
				break;
		}
		$out .= "</td>\n\t</tr>\n";
		$Query->Close();
		return $out;
	}
	
	function Update()
	{
//		print_r($this->Value);
		parent::Update();
	}
}
?>